
#include "../cmd/dataprepare.h"
#include "../mainctrl.h"
#include "commmsg.h"
#include "commclass.h"
#include "CCmdTreeBonus.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_TREE_BONUS, CCmdTreeBonus);

int CCmdTreeBonus::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_WARNING(("inner cmd CCmdTreeBonus get data"));
        return Done(RET_OK);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return Done(RET_SYSTEM_FAIL);
    }

    CallBack(RET_OK);
    return RET_OK;
}

int CCmdTreeBonus::LogicDo(int iRet)
{
    return CallBack(iRet);
}

int CCmdTreeBonus::CallBack(int iRet)
{
    if (_auiGid.Size() == 0)
    {
        return Done(RET_OK);
    }

    if (iRet != RET_OK)
    {
        CT_WARNING(("CCmdTreeBonus ret %d, %u", iRet, _auiGid[0]));
        _auiGid.DelOneItem(0);
    }

    unsigned int uiGid = _auiGid[0];
    PREPARE_PLAYER_BASE_INFO(this, uiGid);
    PREPARE_PLAYER_DETAIL_INFO(this, uiGid);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGid);
    if (iIndex < 0)
    {
        CT_WARNING(("%u", uiGid));
        return CallBack(RET_SYS_ERROR);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGid);
    if (iIndex < 0)
    {
        CT_WARNING(("%u", uiGid));
        return CallBack(RET_SYS_ERROR);
    }
    CPlayerDetailInfo& stPlayerDetailInfo = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iRet = ProcOneUser(stPlayerBaseInfo, stPlayerDetailInfo);
    if (iRet)
    {
        CT_WARNING(("%d, %d", uiGid, iRet));
    }
    
    _auiGid.DelOneItem(0);

    PushQueue(RET_OK);
    return 0;
}


int CCmdTreeBonus::ProcOneUser (CPlayerBaseInfo& stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo)
{
    unsigned int uiGid = stPlayerDetailInfo._uiGID;
    int iIndex = pstMainCtrl->m_stClanMgr.GetClanIndex(uiGid);
    if (iIndex < 0)
    {
        return ERROR_CLAN_NOT_EXIST;
    }
    CClanEntry & stClanEntry = pstMainCtrl->m_stClanMgr._stClanEntryHash[iIndex];

    CTreeData & stTreeData = stClanEntry._stTree;
    CUserTree & stUserTree = stPlayerDetailInfo._stUserClan._stTreeData;
    int iRankIndex = stTreeData.GetRankIndex (uiGid);   

    int iRank = iRankIndex + 1;   //下标为i的元素保存第(i+1)名的成员
    
    int iBigFruitCnt = 0;
    int iSmallFruitCnt = 0;

    int iRet = RET_OK;
    pstMainCtrl->m_stTreeMgr.GetPickCount(iRank, iBigFruitCnt, iSmallFruitCnt);
    for (int ucTreeId = 1; ucTreeId <= MAX_CLAN_TREE_TYPE; ucTreeId++)
    {
        if (pstMainCtrl->m_stTreeMgr.IsTreeUnLock(ucTreeId, stClanEntry._ucClanLevel))
        {
            CTreeOptCnt & stTreeOptCnt = stUserTree.GetTreeOptCnt(ucTreeId);
            iRet = stTreeOptCnt.SetFruit(iBigFruitCnt, iSmallFruitCnt);
            if (iRet)
            {
                CT_ERROR(("%d, %d, %d, %d", iRet, ucTreeId, iBigFruitCnt, iSmallFruitCnt));
            }

            //更新活动列表
            if (pstMainCtrl->m_stPlayerOnline.CheckOnline(uiGid)
                && (iBigFruitCnt > 0 || iSmallFruitCnt > 0))
            {
                pstMainCtrl->m_stActMgr.NotifyActListToRole(stPlayerBaseInfo, stPlayerDetailInfo);
            }
        }
    }  

    //根据神树总等级,发放个人奖励.
    unsigned short usGiftId;
    iRet = pstMainCtrl->m_stTreeMgr.GetTreeBonus (stTreeData, usGiftId);
    if (RET_OK == iRet)
    {
        pstMainCtrl->m_stItemMgr.DropBonus(OSS_MODULE_TREE, uiGid, usGiftId, 1, true);
    }
    else
    {
        CT_ERROR (("%d, %d", iRet, stClanEntry._uiClanId));
    }

    //发送闯关排行奖励

    //保存数据
    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_DETAIL_INFO;
    stKey.m_u64ID = stPlayerDetailInfo._uiGID;
    pstMainCtrl->UpdateData(stKey, stPlayerDetailInfo, VOP_UPDATE);
    
    CT_TRACE(("%d, %d", uiGid, usGiftId));

    return 0;
}
